
# Mel Ó Cinnéide, Jan 2013
# with thanks to Vince Huston for the example

class State_tran
  attr_accessor :age
 def initialize age
   @age = age
   @pool = {}
 end

  def set_age age
    @age = age
  end

  def decide
    if @age < 13
      if !@pool.has_key?'Child'
        @pool['Child'] = Child.new
      end
      return @pool["Child"]
    end
    if (@age>=13 and @age< 18)
      if !@pool.has_key?'Teenager'
        @pool['Teenager'] = Teenager.new
      end
      return @pool["Teenager"]
    end
    if (@age>=18 and @age<65)
      if !@pool.has_key?'Adult'
        @pool['Adult'] = Adult.new
      end
     return @pool["Adult"]
    end
    if (@age>=65)
      if !@pool.has_key?'Pensioner'
        @pool['Pensioner'] = Pensioner.new
      end
      return @pool["Pensioner"]
    end
  end
  end

class Person
  attr_reader :state
  attr_reader :age
	def initialize  
		@age = 0
    @decide = State_tran.new @age
		@state = @decide.decide
	end

  def set_age age
    @age = age
    @decide.set_age age
    @state = @decide.decide
  end

	def incr_age
		@age+=1;
    @decide.age = @age
    @state = @decide.decide
  end

  def vote
    @state.vote
  end

  def apply_for_buspass
    @state.apply_for_buspass
  end

  def conscript
    @state.conscript
  end
  def apply_for_medical_card
    @state.apply_for_medical_card
  end
  end

 class Child

   def vote()
     puts "Too young to vote"
     return "Too young to vote"
   end
   def apply_for_buspass
     puts "Too young for a bus pass"
     return "Too young for a bus pass"
   end
   def conscript
     puts "Too young to be consripted"
     return "Too young to be consripted"
   end
   def apply_for_medical_card
     puts "Qualified for medical card"
     return "Qualified for medical card"
   end
 end

  class Adult
    def vote()
    puts "Vote accepted"
    return "Vote accepted"
    end
    def apply_for_buspass
      puts "Too young for a bus pass"
      return "Too young for a bus pass"
    end
    def conscript
      puts "Here's your gun"
      return "Here's your gun"
    end
    def apply_for_medical_card
      puts "Not qualified for medical card"
      return "Not qualified for medical card"
    end
  end

  class Pensioner
    def vote()
    puts "Vote accepted"
    return "Vote accepted"
    end
    def apply_for_buspass
      puts "Pass granted"
      return "Pass granted"
    end
    def conscript
      puts "Too old to be consripted"
      return "Too old to be consripted"
    end
    def apply_for_medical_card
      puts "Qualified for medical card"
      return "Qualified for medical card"
    end
  end

  class Teenager
    def vote
      puts "Too young to vote"
      return "Too young to vote"
    end
    def apply_for_buspass
      puts "Pass granted"
      return "Pass granted"
    end
    def conscript
      puts "Here's your gun"
      return "Here's your gun"
    end
    def apply_for_medical_card
      puts "Not qualified for medical card"
      return "Not qualified for medical card"
    end
  end




"p = Person.new
for i in 1..80
  p.incr_age();
	p.apply_for_buspass();
	p.vote();
	p.conscript();
end    "